﻿<%@ WebHandler Language="C#" Class="RbPhysicsAndChemicalTestReport" %>

using System;
using System.IO;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Net;
using ReportBusiness;

/**
 * Description: 热表理化试样信息查询报表
 * Copyright (c) : 通力凯顿（北京）系统集成有限公司
 * Writer:zhunan
 * Date: 2022.8.17
 **/

public class RbPhysicsAndChemicalTestReport : IHttpHandler
{
    uMESReportOracleDAL dal = new uMESReportOracleDAL();
    public bool IsReusable { get { return false; } }
    public void ProcessRequest(HttpContext context)
    {
        string requestjson = "";
        using (StreamReader reader = new StreamReader(context.Request.InputStream)) { requestjson = reader.ReadToEnd(); }
        JObject jObject = JsonConvert.DeserializeObject(requestjson) as JObject;
        if (jObject["type"] != null)
        {
            try
            {
                Result r = new Result();
                context.Response.ContentType = "text/json";
                string type = jObject["type"].ToString();
                switch (type)
                {
                    case "getData":
                        r = getData(jObject);//查询
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    case "exportExcel":
                        exportExcel(jObject);//导出
                        break;
                    default:
                        break;
                }
            }
            catch (Exception e)
            {
                Result r = new Result();
                r.data = null;
                r.meta = new Result.Meta(500, e.Message);
                r.total = 0;
                context.Response.ContentType = "text/json";
                context.Response.Write(JsonConvert.SerializeObject(r));
            }
        }
    }

    string getSearchSqlString(JObject jObject)
    {
        string sql = @"
SELECT rb.productname,rb.qty,rb.workflowstepname,rb.testitemname,rb.senddate,rb.estimatedate,rb.notes,
       rb.createdate,e.fullname
FROM rbphysicsandchemicaltest rb
LEFT JOIN employee e ON e.employeeid = rb.createemployeeid
WHERE 1=1
";
        //零件号
        if (jObject["productname"] != null && jObject["productname"].ToString() != "")
        {
            sql += string.Format(" AND rb.productname like '%{0}%'", jObject["productname"].ToString());
        }

        //工序内容
        if (jObject["workflowstepname"] != null && jObject["workflowstepname"].ToString() != "")
        {
            sql += string.Format(" AND rb.workflowstepname LIKE '%{0}%'", jObject["workflowstepname"].ToString());
        }

        //检测项目
        if (jObject["testitemname"] != null && jObject["testitemname"].ToString() != "")
        {
            sql += string.Format(" AND rb.testitemname LIKE '%{0}%'", jObject["testitemname"].ToString());
        }

        //要求完成时间
        if (jObject["estimatedate"] != null && jObject["estimatedate"].ToString() != "")
        {
            JArray arr = JArray.Parse(jObject["estimatedate"].ToString());
            if (arr.Count == 2)
            {
                sql += string.Format(" AND TRUNC(rb.estimatedate) >= TO_DATE('{0}','yyyy-mm-dd')", arr[0].ToString());
                sql += string.Format(" AND TRUNC(rb.estimatedate) <= TO_DATE('{0}','yyyy-mm-dd')", arr[1].ToString());
            }
        }

        //创建时间
        if (jObject["createdate"] != null && jObject["createdate"].ToString() != "")
        {
            JArray arr = JArray.Parse(jObject["createdate"].ToString());
            if (arr.Count == 2)
            {
                sql += string.Format(" AND TRUNC(rb.createdate) >= TO_DATE('{0}','yyyy-mm-dd')", arr[0].ToString());
                sql += string.Format(" AND TRUNC(rb.createdate) <= TO_DATE('{0}','yyyy-mm-dd')", arr[1].ToString());
            }
        }

        //送样时间
        if (jObject["senddate"] != null && jObject["senddate"].ToString() != "")
        {
            JArray arr = JArray.Parse(jObject["senddate"].ToString());
            if (arr.Count == 2)
            {
                sql += string.Format(" AND TRUNC(rb.senddate) >= TO_DATE('{0}','yyyy-mm-dd')", arr[0].ToString());
                sql += string.Format(" AND TRUNC(rb.senddate) <= TO_DATE('{0}','yyyy-mm-dd')", arr[1].ToString());
            }
        }

        //创建人
        if (jObject["fullname"] != null && jObject["fullname"].ToString() != "")
        {
            sql += string.Format(" AND e.fullname LIKE '%{0}%'", jObject["fullname"].ToString());
        }

        return sql;
    }

    //查询方法
    Result getData(JObject jObject)
    {
        Result r = new Result();

        string sql = getSearchSqlString(jObject);

        uMESPagingDataDTO dto = dal.GetPagingDataIns(sql, (int)jObject["currentPage"], (int)jObject["pageSize"]);

        DataTable dt = dto.DBTable;

        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }

        r.data = dt;
        r.total = Convert.ToInt32(dto.RowCount);
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }

    //导出
    void exportExcel(JObject jObject)
    {
        string sql = getSearchSqlString(jObject);
        DataTable dtResult = dal.GetDataTable(sql);

        foreach (DataColumn col in dtResult.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }

        dtResult.Columns["productname"].Caption = "零件号";
        dtResult.Columns["qty"].Caption = "数量";
        dtResult.Columns["workflowstepname"].Caption = "工序内容";
        dtResult.Columns["testitemname"].Caption = "检测项目";
        dtResult.Columns["senddate"].Caption = "送检时间";
        dtResult.Columns["estimatedate"].Caption = "要求完成时间";
        dtResult.Columns["notes"].Caption = "备注";
        dtResult.Columns["createdate"].Caption = "创建时间";
        dtResult.Columns["fullname"].Caption = "创建人";

        ExportHelper.ExportExcel(dtResult, jObject["factoryname"].ToString() + "理化试样信息查询报表");
    }
}